package colors;

import java.io.*;
import java.util.*;

public class Colors 
{

	private static int nbSet = 7;
	private static Colors instance;
	
	private static int colorIndex;
	public static int[] indexArray;
	public static ArrayList<Color>[] colorsArray;
	
	@SuppressWarnings("unchecked")
	private Colors()
	{
		colorsArray = new ArrayList[nbSet];
		indexArray = new int[] {0,0,0,0,0,0,0};
		Random gen = new Random();
		colorIndex = gen.nextInt(nbSet);
		boolean shuffled = false;
		
		colorsArray[0] = loadColors("./bin/colors/blues.col", shuffled);
		colorsArray[1]  = loadColors("./bin/colors/greens.col", shuffled);
		colorsArray[2]  = loadColors("./bin/colors/oranges.col", shuffled);
		colorsArray[3]  = loadColors("./bin/colors/pinks.col", shuffled);
		colorsArray[4]  = loadColors("./bin/colors/reds.col", shuffled);
		colorsArray[5]  = loadColors("./bin/colors/yellows.col", shuffled);
		colorsArray[6]  = loadColors("./bin/colors/colorsSet.col", shuffled);
	}
	
	private static ArrayList<Color> loadColors(String pathFile, boolean shuffeld)
	{
		try 
		{
			ArrayList<Color>colors = new ArrayList<Color>();
			BufferedReader stream = new BufferedReader(new FileReader(pathFile));
			String line;
			while ((line = stream.readLine()) != null)   
			{
				line = line.replaceAll("\t", "");
				line = line.replaceAll(" ", "");
				String[] strColor = line.split("-"); 
				colors.add(new Color(Integer.parseInt(strColor[0]), Integer.parseInt(strColor[1]), Integer.parseInt(strColor[2])));
			}
			
			if(shuffeld == true)
			{
				colors = Colors.shuffle(colors);
			}
			stream.close();
			
			return colors;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ArrayList<Color>();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ArrayList<Color>();
		}		
	}
	
	private static Colors getInstance()
	{
		if(instance == null)
		{
			Colors.instance = new Colors();
		}
		
		return Colors.instance;
	}
	
	private static Color Next(int index)
	{
		Colors.getInstance();
		if(indexArray[index] >= colorsArray[index].size())
		{
			indexArray[index] = 0;
		}
		
		return colorsArray[index].get(indexArray[index]++);
	}
	
	public static Color NextBlue()
	{
		Colors.getInstance();
		return Colors.Next(0);
	}
	public static Color NextGreen()
	{
		Colors.getInstance();
		return Colors.Next(1);
	}
	public static Color NextOrange()
	{
		Colors.getInstance();
		return Colors.Next(2);
	}	
	public static Color NextPink()
	{
		Colors.getInstance();
		return Colors.Next(3);
	}
	public static Color NextRed()
	{
		Colors.getInstance();
		return Colors.Next(4);
	}	
	public static Color NextYellow()
	{
		Colors.getInstance();
		return Colors.Next(5);
	}
	
	public static Color NextColorSet()
	{
		Colors.getInstance();
		return Colors.Next(6);
	}
	
	public static Color Next()
	{
		Colors.getInstance();
		return Next(colorIndex++ % nbSet);
	}
	
	public static <T> ArrayList<T> shuffle(ArrayList<T> list)
	{
	    if (list.size() > 0) 
	    {
	        ArrayList<T> copy = new ArrayList<T>();
	        for (T object : list)
	        {
	            copy.add(object);
	        }
	        
	        Random generator = new Random();
	        ArrayList<T> result = new ArrayList<T>();
	        do
	        {
	            int index = (int) (generator.nextDouble() * (double) copy.size());
	            result.add(copy.remove(index));
	        } while (copy.size() > 0);
	        
	        return result;
	    } 
	    else
	    {
	        return new ArrayList<T>();
	    }
	}
}
